
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Karthik T
 */
public final class Paddle {

    public Sprite paddleSprite,paddleCCW,paddleCW,paddleN;
    private int screen_limit;
    /* Paddle position */
    private int posX;
    private int posY;
    private int velX;
    private int score;

    public int shift = 6;
    private int position;
    private final int CCW=1;
    private final int CW=-1;
    private final int N=0;
	
    private int maxScore = 5;
    private String name;

    private int rotate = N;
    public Paddle(int screen_width,int screen_height,int position,Image paddleImage,Image paddleCCW,Image paddleCW,String name) {
        screen_limit=screen_width;
        paddleSprite=paddleN= new Sprite(paddleImage);
        this.paddleCCW = new Sprite(paddleCCW);
        this.paddleCW = new Sprite(paddleCW);
        posX=screen_limit/2-paddleSprite.getWidth()/2;
        posY = position==MainGame.BOTTOM?screen_height-25:20;
        resetScore();
        updateSprite();
        this.position=position;
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public int getScore(){
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void incScore() throws InterruptedException{
        score++;
        if(score==maxScore){
            if(position==MainGame.TOP)MainGame.gameOver(false);
            else MainGame.gameOver(true);
        }
    }

    public void resetScore(){
        score=0;
    }
    public void updateSprite(){
        paddleSprite.setPosition(posX, rotate ==0?posY:posY-21);
    }

    public void move_right(){
        posX+=shift;
        if(velX!=1){
            velX=1;
            sendPaddleDet();
        }
        if(posX>screen_limit-paddleSprite.getWidth())posX=screen_limit-paddleSprite.getWidth();
        updateSprite();
    }

    public void move_left(){
        posX-=shift;
        if(velX!=-1){
            velX=-1;
            sendPaddleDet();
        }
        if(posX<0)posX=0;
        updateSprite();
    }
    /*public void ai_move(int diff){
    if(diff < 0)
    posX+=4;
    else
    posX-=4;
    if(posX>screen_limit-paddleSprite.getWidth())posX=screen_limit-paddleSprite.getWidth();
    updateSprite();
    }*/
    public int getPosX(){
       return posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public int getVelX() {
        return velX;
    }

    public void setVelX(int velX) {
        this.velX = velX;
    }

    void simulate() {
        if(velX>0)move_right();
        else if (velX<0) move_left();
    }
    void sendPaddleDet(){
        if(MainGame.mode!=MainGame.SINGLE)MobilePong.connectMan.sendPaddleDet();
    }

    void stop() {
        if(velX!=0){
            velX=0;
            sendPaddleDet();
        }
    }

    boolean rotateCCW() {
        if(rotate!=CCW){
            rotate=CCW;
            updateSpriteImage();
            updateSprite();
            return(true);
        }return(false);
    }

    boolean rotateCW() {
        if(rotate!=CW){
            rotate=CW;
            updateSpriteImage();
            updateSprite();
            return(true);
        }return(false);
    }

    boolean rotateN() {
        if(rotate!=N){
            rotate = N;
            updateSpriteImage();
            updateSprite();
            return(true);
        }return(false);
    }

    private void updateSpriteImage() {
        switch(rotate){
            case CW:{
                MainGame.manager.remove(paddleSprite);
                paddleSprite=paddleCW;
                MainGame.manager.insert(paddleCW, 1);
            }break;

            case N:{
                MainGame.manager.remove(paddleSprite);
                paddleSprite=paddleN;
                MainGame.manager.insert(paddleN, 1);
            }break;
            case CCW:{
                MainGame.manager.remove(paddleSprite);
                paddleSprite=paddleCCW;
                MainGame.manager.insert(paddleCCW, 1);
            }break;
            default:System.out.println("Wierd Rotation - "+rotate);
        }
    }

    void handleCollide(ball Ball) {
        if(rotate==N){
            Ball.setVelY(-Ball.getVelY());
        }else if(rotate==CCW){
            int oldX=Ball.getVelX(),oldY=Ball.getVelY();
            Ball.setVelX(-oldY);
            Ball.setVelY(oldX);
        }else{
            int oldX=Ball.getVelX(),oldY=Ball.getVelY();
            Ball.setVelX(oldY);
            Ball.setVelY(-oldX);
        }
    }
}
